package com.razorthink.shell.utils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import com.razorthink.deployment.fabric.core.FabricSourceBuilder;
import com.razorthink.deployment.fabric.core.Module;
import com.razorthink.shell.core.entities.Config;

public class FabricExecutor {
	public static String execute(Config c, ArrayList<Module> modules,
			String execModule) throws IOException, InterruptedException {
		FabricSourceBuilder f = new FabricSourceBuilder();
		f.setUsername(c.getREMOTE_USERNAME());
		f.setPassword(c.getREMOTE_PASSWORD());
		f.setPemKeyfile(c.getPEM_KEY_FILE());
		f.setRemoteHostAddress(c.getREMOTE_HOST_ADDRESS());
		for (Module m : modules) {
			f.addModule(m);
		}
		File generated = SourceFileGenerator.getGeneratedFile(
				c.getLOCAL_TMP_WORKING_DIR(), execModule, "py");
		f.generatePythonSourceFile(generated);
		String runFabric = "fab -f " + generated.getAbsolutePath()
				+ " -R server " + execModule;
		String response = NativeCommandExecutor.execute(runFabric);
		SourceFileGenerator.deleteGeneratedFiles(generated);
		return response;
	}
}
